package user

import (
	"encoding/json"
	"vblog/common"
)

// 需要存放到数据库里的字段
// Id
// CreateAt
// UpdateAt

// Username
// Password
// Role
// Label

type (
	CreateUserRequest struct {
		Username string            `json:"username,omitempty" validate:"required,min=3,max=100" label:"用户名"`
		Password string            `json:"password,omitempty" validate:"required,min=5,max=100" label:"密码"`
		Role     Role              `json:"role,omitempty"`
		Label    map[string]string `json:"label,omitempty" gorm:"serializer:json"`
	}

	User struct {
		*common.Meta
		*common.UpdateAtRequest
		*CreateUserRequest
	}

	UserSet struct {
		Total int64
		Items []*User
	}
)

// 构造函数
func NewCreateUserRequest() *CreateUserRequest {
	return &CreateUserRequest{
		Label: make(map[string]string),
	}
}

func NewUser(c *CreateUserRequest) *User {
	return &User{
		Meta:              common.NewMeta(),
		CreateUserRequest: c,
	}
}

func (u *User) String() string {
	b, _ := json.MarshalIndent(u, " ", " ")
	return string(b)
}

func (u *User) Table() string {
	return AppName
}

func NewUserSet() *UserSet {
	return &UserSet{
		Items: make([]*User, 10),
	}
}

func (s *UserSet) String() string {
	b, _ := json.MarshalIndent(s, " ", " ")
	return string(b)
}

// func (c *CreateUserRequest) HashPassword() error {
// 	b, err := bcrypt.GenerateFromPassword([]byte(c.Password), bcrypt.MinCost)
// 	if err != nil {
// 		return err
// 	}
// 	c.Password = string(b)
// 	return nil
// }
